<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Changes the default external entity loader</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.libxml-get-last-error.html">« libxml_get_last_error</a></li>
      <li style="float: right;"><a href="function.libxml-set-streams-context.html">libxml_set_streams_context »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.libxml.html">libxml 函数</a></li>
    <li>Changes the default external entity loader</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.libxml-set-external-entity-loader" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">libxml_set_external_entity_loader</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.4.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">libxml_set_external_entity_loader</span> &mdash; <span class="dc-title">Changes the default external entity loader</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.libxml-set-external-entity-loader-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>libxml_set_external_entity_loader</strong></span>(<span class="methodparam"><span class="type">?</span><span class="type"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span><span class="type"></span></span> <code class="parameter">$resolver_function</code></span>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   Changes the default external entity loader.
   This can be used to suppress the expansion of arbitrary external entities to avoid XXE attacks,
   even when <strong><code>LIBXML_NOENT</code></strong> has been set for the respective operation,
   and is usually preferable over calling <span class="function"><a href="function.libxml-disable-entity-loader.html" class="function">libxml_disable_entity_loader()</a></span>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.libxml-set-external-entity-loader-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">resolver_function</code></dt>

    <dd>

     <p class="para">
      A <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> with the following signature:
      <div class="methodsynopsis dc-description">
       <span class="methodname"><span class="replaceable">resolver</span></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$public_id</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$system_id</code></span>, <span class="methodparam"><span class="type">array</span> <code class="parameter">$context</code></span>): <span class="type"><span class="type">resource</span>|<span class="type">string</span>|<span class="type">null</span></span></div>

      <dl>
       
        <dt>
<code class="parameter">public_id</code></dt>

        <dd>

         <span class="simpara">
          The public ID.
         </span>
        </dd>

       
       
        <dt>
<code class="parameter">system_id</code></dt>

        <dd>

         <span class="simpara">
          The system ID.
         </span>
        </dd>

       
       
        <dt>
<code class="parameter">context</code></dt>

        <dd>

         <span class="simpara">
          An array with the four elements <code class="literal">&quot;directory&quot;</code>, <code class="literal">&quot;intSubName&quot;</code>,
          <code class="literal">&quot;extSubURI&quot;</code> and <code class="literal">&quot;extSubSystem&quot;</code>.
         </span>
        </dd>

       
      </dl>

      This callable should return a <a href="language.types.resource.html" class="link">资源(resource)</a>, a <span class="type">string</span> from which a resource can be
      opened. If <strong><code>null</code></strong> is returned, the entity reference resolution will fail.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.libxml-set-external-entity-loader-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>true</code></strong>， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.libxml-set-external-entity-loader-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5434">
    <p><strong>示例 #1 <span class="function"><strong>libxml_set_external_entity_loader()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$xml&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;XML<br /></span><span style="color: #DD0000">&lt;!DOCTYPE&nbsp;foo&nbsp;PUBLIC&nbsp;"-//FOO/BAR"&nbsp;"http://example.com/foobar"&gt;<br />&lt;foo&gt;bar&lt;/foo&gt;<br /></span><span style="color: #007700">XML;<br /><br /></span><span style="color: #0000BB">$dtd&nbsp;</span><span style="color: #007700">=&nbsp;&lt;&lt;&lt;DTD<br /></span><span style="color: #DD0000">&lt;!ELEMENT&nbsp;foo&nbsp;(#PCDATA)&gt;<br /></span><span style="color: #007700">DTD;<br /><br /></span><span style="color: #0000BB">libxml_set_external_entity_loader</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;(</span><span style="color: #0000BB">$public</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$system</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">)&nbsp;use(</span><span style="color: #0000BB">$dtd</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$public</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$system</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$f&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://temp"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r+"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$f</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dtd</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">rewind</span><span style="color: #007700">(</span><span style="color: #0000BB">$f</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$f</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />);<br /><br /></span><span style="color: #0000BB">$dd&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$r&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dd</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #0000BB">$xml</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$dd</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">validate</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(10) &quot;-//FOO/BAR&quot;
string(25) &quot;http://example.com/foobar&quot;
array(4) {
    [&quot;directory&quot;]    =&gt; NULL
    [&quot;intSubName&quot;]   =&gt; NULL
    [&quot;extSubURI&quot;]    =&gt; NULL
    [&quot;extSubSystem&quot;] =&gt; NULL
}
bool(true)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.libxml-set-external-entity-loader-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.libxml-disable-entity-loader.html" class="function" rel="rdfs-seeAlso">libxml_disable_entity_loader()</a> - Disable the ability to load external entities</span></li>
    <li class="member"><span class="function"><a href="function.libxml-get-external-entity-loader.html" class="function" rel="rdfs-seeAlso">libxml_get_external_entity_loader()</a> - Get the current external entity loader</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>